// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-05 16:38:14
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:55:57
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Node/SPOINTParser.cpp
 */
#include "SPOINTParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Node/ISPOINTService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (SPOINT);
/*
// file://./../../Doc/MarkDown/BulkData/GRID/GRID.md>
*/

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSPOINT (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                dividedEntryInfo)
{
    // 定义一个 SPCData 的集合，用于存储解析出来的数据
    std::vector<SG::DataStructure::FEM::SPOINTData> datas;
    SG::DataStructure::FEM::SPOINTData              tempData;  // 用于临时存储每个约束信息

    // TODO(bug 001) 填写正确的 m_domainId，目前暂时设置为 1
    tempData.m_domainId = 1;

    // 标记当前处理的节点是否为 "THRU" 形式
    bool isTHRUStr = false;

    // 计算分割条目的总行数，每行 10 个字段
    size_t const allRowNum = dividedEntryInfo.size () / 10;

    // 遍历每一行条目信息
    for (size_t curRow = 0; curRow < allRowNum; curRow++)
    {
        // 遍历当前行的每一个字段
        for (size_t fieldsColumn = 1; fieldsColumn < 9; fieldsColumn++)
        {
            // 获取当前字段的索引
            const size_t curIndex  = curRow * 10 + fieldsColumn;
            std::string& curStrKey = dividedEntryInfo[curIndex];

            // 判断当前字段是否为空，如果为空则跳过
            bool const isEmpty = is_all_space (curStrKey);
            if (isEmpty)
            {
                continue;
            }

            // 检查当前字段是否包含 "THRU" 标记
            if (boost::icontains (curStrKey, "THRU"))
            {
                isTHRUStr = true;  // 标记当前处理的是 "THRU"
                continue;
            }

            // 处理 "THRU" 语法
            if (isTHRUStr)
            {
                /** "THRU" 前的一个约束已经保存到数据 `datas` 中
                 *  取 `datas` 中最后一个约束的节点加 1，作为 THRU 代表的第一个点
                 *  读取 "THRU" 后一个节点编号，作为最后一个约束的节点号
                 */
                SG::DataStructure::Common::I64 const beginId = datas.back ().m_id + 1;
                SG::DataStructure::Common::I64 const endID   = convertToI64 (dividedEntryInfo[curIndex]);

                // 遍历从 `beginId` 到 `endID` 的所有节点，将它们添加到 `datas` 中
                for (SG::DataStructure::Common::I64 curId = beginId; curId <= endID; curId++)
                {
                    tempData.m_id = curId;       // 设置节点号
                    datas.push_back (tempData);  // 保存约束数据
                }

                // 完成 "THRU" 语法的处理，重置 isTHRUStr
                isTHRUStr = false;
            }
            else
            {
                // 不包含 "THRU" 的情况，直接处理节点
                tempData.m_id = convertToI64 (dividedEntryInfo[curIndex]);  // 解析并设置节点号
                datas.push_back (tempData);                                 // 保存约束数据
            }
        }
    }

    // 遍历保存的约束数据，并将其添加到数据库中
    for (auto& data : datas)
    {
        if (dbServiceFactory->get<ISPOINTService> ()->Add (data) == Common::Status::STATUS_Failure)
        {
            throw std::runtime_error ("Failed to add SPOINT data to database.");
            return Common::Status::STATUS_Failure;
        }
    }

    return Common::Status::STATUS_Success;
}